package com.gscitysfy.cus.drain.utils;

import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName HttpResponseUtil
 * @Description 设置上传下载响应头
 * @creatTime 2022/3/21 09:06
 */
public class HttpResponseUtil {

    /**
     * 获取下载文件的响应头并编码文件
     */
    public static HttpHeaders getDownloadHeaders(String filename) throws UnsupportedEncodingException {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename= \"" + new String(filename.getBytes(), "ISO8859-1") + "\"");
        headers.add("Content-Type","application/octet-stream");
        headers.add("Content-Transfer-Encoding", "binary");
        headers.add("Content-Description", "File Transfer");
        headers.add("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        headers.add("Pragma", "public");
        return headers;
    }

    /**
     * 设置下载文件的响应头并编码文件
     */
    public static void setResponseHeaders(HttpServletResponse response, String filename) throws UnsupportedEncodingException {
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "ISO8859-1"));
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
    }
}
